/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.library.image.AbstractImageFilter;

/**
 * ImageDisplayNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class ImageDisplayNode extends AbstractImageFilter {

	/**
	 * maxDisplayWidth
	 */
	private InputPort maxDisplayWidth;

	/**
	 * maxDisplayHeight
	 */
	private InputPort maxDisplayHeight;

	/**
	 * ImageDisplayNode Constructor
	 * 
	 */
	public ImageDisplayNode() {
		this("DISPLAY" + nr++, false);
	}

	/**
	 * ImageDisplayNode Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public ImageDisplayNode(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	@Override
	public void compute() {
		BufferedImage bi = (BufferedImage) fetchAndForwardData(in, out);

		int dWidth = (Integer) maxDisplayWidth.fetchData();
		int dHeight = (Integer) maxDisplayHeight.fetchData();

		// display the image
		final JFrame frame = new JFrame(getNodeName());

		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		Container c = frame.getContentPane();
		c.setLayout(new BorderLayout());

		final JButton closeButton = new JButton("Close");
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
				frame.dispose();
			}
		});
		frame.add(closeButton, BorderLayout.SOUTH);

		frame.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {
			}

			public void keyReleased(KeyEvent e) {
			}

			public void keyPressed(KeyEvent e) {
				closeButton.doClick();
			}
		});

		JLabel icon = new JLabel(new ImageIcon(bi));
		icon.setToolTipText("<html>" + bi.getWidth() + "x" + bi.getHeight() + "<br/>" + bi.getColorModel()
				+ "</html>");
		int width = icon.getIcon().getIconWidth();
		width = width > dWidth ? dWidth : width;
		int height = icon.getIcon().getIconHeight();
		height = height > dHeight ? dHeight : height;
		icon.setMaximumSize(new Dimension(width, height));
		c.add(new JScrollPane(icon, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);

		frame.pack();
		frame.setVisible(true);
	}

	/**
	 * @see de.teamskill.pux.library.image.AbstractImageFilter#createPorts()
	 */
	@Override
	protected void createPorts() {
		super.createPorts();
		maxDisplayWidth = addConfigurationPort("maxDisplayWidth", Integer.class, new Integer(300));
		maxDisplayHeight = addConfigurationPort("maxDisplayHeight", Integer.class, new Integer(300));
	}

}
